<?php
class ModelConfigMaterial extends Model {
	public function addMaterial($data) {
		$sql = "INSERT INTO `" . DB_PREFIX . "config_material` SET ".
		"name = '" . $this->db->escape($data['name'])."'";
		echo $sql;
		$this->db->query($sql);
	}
	
	public function editMaterial($material_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "config_material` SET ".
		"name = '" . $this->db->escape($data['name']) ."' WHERE material_id = '" . (int)$material_id . "'");
	}
	
	
	public function getTotalMaterials() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_material`");
		return $query->row['total'];
	}
	
	public function getMaterial($material_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "config_material` WHERE material_id = '" . (int)$material_id . "'");
		return $query->row;
	}
	
	public function getAllMaterials($data=array()){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_material`";
			
		$sort_data = array(
			'date_added'
		);	
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function deleteMaterial($material_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "config_material` WHERE material_id = '" . (int)$material_id . "'");
	}
	
	public function getMaterials($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_material`";
			
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
}
?>